package com.ash.service.chart;

import com.ash.domain.Chart;
import com.ash.domain.vo.ChartVo;
import com.ash.mapper.chart.ChartMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/4/12 16:14
 * @file : project06.com.ash.service.chart.ChartServiceImpl.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Service
public class ChartServiceImpl implements ChartService {
    @Resource
    ChartMapper chartMapper;

    @Override
    public List<ChartVo> selectByUid(Integer uid, Integer page, Integer limit, String order) {
        if(page!=null&&limit!=null){
            return chartMapper.selectByUid(uid,(page-1)*limit,limit,order);
        }
        return chartMapper.selectByUid(uid, null, null, order);
    }

    @Override
    public Integer selectDataCount(Integer uid) {
        return chartMapper.selectDataCount(uid);
    }

    @Override
    public Integer deleteByBids(Integer uid, List<Integer> bids) {
        return chartMapper.deleteByBids(uid, bids);
    }

    @Override
    public Integer add(Chart chart) {
        Chart chart1 = chartMapper.selectByBid(chart.getUid(), chart.getBid());
        if (chart1 != null) {
            chart.setCount(chart.getCount() + chart1.getCount());
            return chartMapper.setCount(chart);
        } else {
            return chartMapper.add(chart);
        }
    }

    @Override
    public Chart selectByBid(Integer uid, Integer bid) {
        return chartMapper.selectByBid(uid, bid);
    }

    @Override
    public Integer setCount(Chart chart) {
        return chartMapper.setCount(chart);
    }

    @Override
    public Integer deleteByBid(Integer uid, Integer bid) {
        return chartMapper.deleteByBid(uid,bid);
    }

    @Override
    public BigDecimal settleAccount(List<Integer> ids, Integer uid) {
        List<ChartVo> chartVos = chartMapper.selectByUidAndBids(uid, ids);
        BigDecimal count=new BigDecimal(0);
        for (ChartVo chartVo : chartVos) {
            BigDecimal multiply = chartVo.getPrice().multiply(new BigDecimal(chartVo.getCount()));
            count = count.add(multiply);
        }
        return count;
    }

    @Override
    public List<ChartVo> selectByUidAndBids(List<Integer> ids, Integer uid) {
        return chartMapper.selectByUidAndBids(uid,ids);
    }
}
